/**
 * @author khathirdstep1025
 * Servlet handles input validation.
 */

package com.kha.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kha.Database;
import com.kha.Trainee;

/**
 * Servlet implementation class ValidateServlet
 */
@WebServlet({ "/ValidateServlet" })
public class ValidateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	// Database configuration.
	private String CONNECTION_URL = "";
	private String DB_Driver = "";

	/**
	 * Servlet Init
	 */
	@Override
	public void init(ServletConfig config) throws ServletException {

		ServletContext context = config.getServletContext();

		CONNECTION_URL = context.getInitParameter("CONNECTION_URL")
				+ "?user=root&;password=";
		DB_Driver = context.getInitParameter("DB_Driver");

		super.init(config);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// Same as doGet.
		doGet(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// Get session object.
		HttpSession session = request.getSession(true);

		// It's time to validate.
		String account = request.getParameter("txtAccount");
		String firstname = request.getParameter("txtFirstName");
		String lastname = request.getParameter("txtLastName");
		String email = request.getParameter("txtEmail");

		// Get or create bean.
		Trainee trainee = (Trainee) session.getAttribute("trainee");

		if (trainee == null) {
			trainee = new Trainee();
			session.setAttribute("trainee", trainee);
		}

		// Update data.
		trainee.setAccount(account.trim());
		trainee.setFirstname(firstname.trim());
		trainee.setLastname(lastname.trim());
		trainee.setEmail(email.trim());

		if (account == null || "".equals(account.trim())) {

			trainee.setAccount("");

			// Add request attribute to notify user.
			request.setAttribute("account_message", "Account");

		} else {
			trainee.setAccount(account.trim());
		}

		if (firstname == null || "".equals(firstname.trim())) {

			trainee.setFirstname("");

			// Add request attribute to notify user.
			request.setAttribute("firstname_message", "Firstname");

		} else {
			// Update well-formatted information.
			trainee.setFirstname(firstname.trim());
		}

		if (lastname == null || "".equals(lastname.trim())) {

			trainee.setLastname("");

			// Add request attribute to notify user.
			request.setAttribute("lastname_message", "Lastname");

		} else {
			// Update well-formatted information.
			trainee.setLastname(lastname.trim());
		}

		if (email == null || "".equals(email.trim())) {

			trainee.setEmail("");

			// Add request attribute to notify user.
			request.setAttribute("email_message", "Email");

		} else {
			// Update well-formatted information.
			trainee.setEmail(email.trim());
		}

		if (trainee.isValid() != true) {

			// Forward to homepage in validation failed.
			RequestDispatcher dispatch = request
					.getRequestDispatcher("index.jsp");
			dispatch.forward(request, response);

		} else {

			// Populate list of trainee, pass it to trainees.jsp to show up.
			Database db = new Database(CONNECTION_URL, DB_Driver);

			ArrayList<Trainee> list = null;
			try {
				list = db.getTraineeList();
			} catch (InstantiationException e) {
			} catch (IllegalAccessException e) {
			} catch (ClassNotFoundException e) {
			} catch (SQLException e) {
			}

			if (list != null) {

				
				// Forward to trainees with the list.
				request.setAttribute("result", list);

				RequestDispatcher dispatch = request
						.getRequestDispatcher("trainees.jsp");
				dispatch.forward(request, response);
			}
		}
	}
}
